home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / BoxMooV / sources / BoxMooV_Texture.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-14  |  6.4 KB  |  278 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    BoxMooV_Texture.c
  3.  *
  4.  *    Rick Evans Sept. 1996                            
  5.  *    Robert Dierkes                                                                                
  6.  *    (c)1994-96 Apple Computer Inc., All Rights Reserved                                
  7.  *    
  8. */
  9.  
  10. /*------------------*/
  11. /*    Include Files    */
  12. /*------------------*/
  13. #include <QuickDraw.h>
  14. #include <Resources.h>
  15.  
  16. #include "QD3D.h"
  17. #include "QD3DGroup.h"
  18. #include "QD3DShader.h"
  19. #include "QD3DSet.h"
  20.  
  21. #include "BoxMooV_Texture.h"
  22. #include "BoxMooV_QuickTime.h"
  23.  
  24. /*--------------------------*/
  25. /*      Private prototypes    */
  26. /*--------------------------*/
  27.  
  28. TQ3Status    AddTextureToGroup(TQ3GroupObject group, TQ3StoragePixmap *pStoragePixmap);
  29.  
  30. /*--------------*/
  31. /*    Routines    */
  32. /*--------------*/
  33.  
  34. /*===========================================================================*\
  35.  *
  36.  *    Routine:    Texture_New()
  37.  *
  38.  *    Comments:    Create a new Animated Texture
  39.  *                Set its fMovie field to a new Movie provided by user.
  40.  *                Create a new GWorld with dimension based on the Moive's bounds.
  41.  *                Set the Movie to draw into the GWorld.
  42.  *                Create a new Storage Pixmap based on the GWorld
  43.  *                Start movie playing in a loop.
  44.  *
  45. \*===========================================================================*/
  46.  
  47. TAnimatedTextureHdl Texture_New(
  48.             void)
  49. {
  50.     unsigned long        pictMapAddr;
  51.     GWorldPtr             pGWorld;
  52.     PixMapHandle         hPixMap;
  53.     unsigned long         pictRowBytes;
  54.     QDErr                err;
  55.     GDHandle            oldGD;
  56.     GWorldPtr            oldGW;
  57.     Boolean                success;
  58.     Rect                bounds;
  59.     TQ3StoragePixmap    *strgPMap;
  60.     TAnimatedTextureHdl theAnimTxtr = (TAnimatedTextureHdl)NewHandleClear(sizeof(TAnimatedTexture));
  61.  
  62.     HLock((Handle)theAnimTxtr);
  63.  
  64.     /* save current port */
  65.     GetGWorld(&oldGW, &oldGD);
  66.  
  67.     QuickTime_Init();
  68.     
  69.     success = QuickTime_GetNewMooVTexture(&(**theAnimTxtr).fMovie);  // Will prompt for movie file.
  70.     if (!success) goto bail;
  71.  
  72.     GetMovieBox((**theAnimTxtr).fMovie, &bounds);
  73.     
  74.     err = NewGWorld(&pGWorld, 32, &bounds, 0, 0, useTempMem);
  75.     if (err != noErr) {
  76.         QuickTime_Delete(&(**theAnimTxtr).fMovie);
  77.         success = false;
  78.         goto bail;
  79.     }
  80.  
  81.     success = true;
  82.  
  83.     hPixMap = GetGWorldPixMap(pGWorld);
  84.     LockPixels(hPixMap);
  85.     pictMapAddr  = (unsigned long)GetPixBaseAddr (hPixMap);
  86.     pictRowBytes = (unsigned long)(**hPixMap).rowBytes & 0x3fff;
  87.  
  88.     SetGWorld(pGWorld, nil);
  89.  
  90.     strgPMap = &(**theAnimTxtr).fStoragePixmap;
  91.     strgPMap->image = Q3MemoryStorage_NewBuffer(    (void *) pictMapAddr,
  92.                                                     pictRowBytes * bounds.bottom,
  93.                                                     pictRowBytes * bounds.bottom);
  94.     strgPMap->width        = bounds.right;
  95.     strgPMap->height    = bounds.bottom;
  96.     strgPMap->rowBytes    = pictRowBytes;
  97.     strgPMap->pixelSize = 32;
  98.     strgPMap->pixelType = kQ3PixelTypeRGB32;
  99.     strgPMap->bitOrder    = kQ3EndianBig;
  100.     strgPMap->byteOrder = kQ3EndianBig;
  101.  
  102.     QuickTime_LoopMovie((**theAnimTxtr).fMovie, pGWorld);
  103.  
  104.     (**theAnimTxtr).fpGWorld = pGWorld;
  105.  
  106. bail:
  107.     SetGWorld(oldGW, oldGD);
  108.  
  109.     HUnlock((Handle)theAnimTxtr);
  110.  
  111.     if (success)
  112.         return theAnimTxtr;
  113.     else
  114.         return NULL;
  115. }
  116.  
  117. /*===========================================================================*\
  118.  *
  119.  *    Routine:    AddTextureToGroup()
  120.  *
  121.  *    Comments:    Create a new textureShader based on the pixmap storage data.
  122.  *                Add it to the group.
  123.  *                
  124.  *
  125. \*===========================================================================*/
  126.  
  127. TQ3Status AddTextureToGroup(
  128.             TQ3GroupObject        pGroup,
  129.             TQ3StoragePixmap    *pStoragePixmap)
  130. {
  131.     TQ3TextureObject    textureObject;
  132.     
  133.     textureObject = Q3PixmapTexture_New(pStoragePixmap);
  134.     if (textureObject != NULL)
  135.     {
  136.         /* New Texture Shader */
  137.         TQ3ShaderObject    textureShader;
  138.         
  139.         textureShader = Q3TextureShader_New(textureObject);
  140.         Q3Object_Dispose(textureObject);
  141.         
  142.         if (textureShader != NULL)
  143.         {
  144.             TQ3GroupPosition    position;
  145.             
  146.             Q3Group_GetFirstPosition(pGroup, &position);
  147.             Q3Group_AddObjectBefore(pGroup, position, textureShader);
  148.             Q3Object_Dispose(textureShader);
  149.             return kQ3Success;
  150.         }
  151.         else {
  152.             /*ERROR_DEBUG_STR("AddTextureToGroup: Q3TextureShader_New failed.");*/
  153.         }
  154.     }
  155.     else {
  156.         /*ERROR_DEBUG_STR("AddTextureToGroup: Q3PixmapTexture_New failed.");*/
  157.     }
  158.  
  159.     return kQ3Failure;
  160. }
  161.  
  162.  
  163. /*===========================================================================*\
  164.  *
  165.  *    Routine:    Texture_AddToGroup()
  166.  *
  167.  *    Comments:    Add fStoragePixmap to the group.
  168.  *                    
  169.  *
  170. \*===========================================================================*/
  171.  
  172. TQ3Status Texture_AddToGroup(
  173.             TAnimatedTextureHdl    pAnimTxtr,
  174.             TQ3GroupObject        pGroup)
  175. {
  176.     TQ3Status    status;
  177.  
  178.     status = kQ3Failure;
  179.  
  180.     if (pAnimTxtr    == NULL ||
  181.         pGroup        == NULL)
  182.     {
  183.         /*ERROR_DEBUG_STR("AddMovieTextureToGroup: invalid parameters.");*/
  184.         return status;
  185.     }
  186.  
  187.     status = AddTextureToGroup(pGroup, &(**pAnimTxtr).fStoragePixmap);
  188.  
  189.     return status;
  190. }
  191.  
  192. /*===========================================================================*\
  193.  *
  194.  *    Routine:    Texture_Delete()
  195.  *
  196.  *    Comments:    Dellocate the TAnimatedTexture.
  197.  *                    Stop and delete fMovie, 
  198.  *                    free fGWorld's memory,
  199.  *                    and free memory used by fStorageMap.
  200.  *
  201. \*===========================================================================*/
  202.  
  203. Boolean    Texture_Delete(
  204.             TAnimatedTextureHdl    pAnimTxtr)
  205. {
  206.     if ((**pAnimTxtr).fpGWorld == NULL)
  207.         return false;
  208.  
  209.     HLock((Handle)pAnimTxtr);
  210.  
  211.     QuickTime_Delete(&(**pAnimTxtr).fMovie);
  212.  
  213.     DisposeGWorld((**pAnimTxtr).fpGWorld);
  214.     (**pAnimTxtr).fpGWorld = NULL;
  215.  
  216.     if ((**pAnimTxtr).fStoragePixmap.image != NULL)
  217.     {
  218.         Q3Object_Dispose((**pAnimTxtr).fStoragePixmap.image);
  219.         (**pAnimTxtr).fStoragePixmap.image = NULL;
  220.     }
  221.  
  222.     HUnlock((Handle)pAnimTxtr);
  223.     return true;
  224. }
  225.  
  226. /*===========================================================================*\
  227.  *
  228.  *    Routine:    Texture_NextFrame()
  229.  *
  230.  *    Comments:    Advance the texture's movie to next frame
  231.  *                and notify QD3D that the texture's image has changed.
  232.  *                    
  233.  *
  234. \*===========================================================================*/
  235.  
  236. Boolean Texture_NextFrame(
  237.         TAnimatedTextureHdl    pAnimTxtr)
  238. {
  239.     
  240.     TQ3StoragePixmap    *pStrgPMap;
  241.     PixMapPtr            pPixMap;
  242.     long                size;
  243.     TQ3Status            status;
  244.  
  245.     /* If fGWorld is non-NULL then the fMovie is non-NULL
  246.      * and the movie or video needs updating
  247.      */
  248.     if ((**pAnimTxtr).fpGWorld == NULL)
  249.         return false;
  250.  
  251.     HLock((Handle)pAnimTxtr);
  252.  
  253.     if ((**pAnimTxtr).fMovie)
  254.     {
  255.         /* Draw next movie frame */
  256.         MoviesTask((**pAnimTxtr).fMovie, 0);
  257.     }
  258.  
  259.     pStrgPMap = &(**pAnimTxtr).fStoragePixmap;
  260.  
  261.     /* Tell QD3D the buffer changed */
  262.     pPixMap = *((**pAnimTxtr).fpGWorld)->portPixMap;
  263.     size    = pStrgPMap->height * pStrgPMap->rowBytes;
  264.  
  265.     status = Q3MemoryStorage_SetBuffer(
  266.         pStrgPMap->image,
  267.         (void *) pPixMap->baseAddr,
  268.         size,
  269.         size);
  270.  
  271.     HUnlock((Handle)pAnimTxtr);
  272.     
  273.     if (status == kQ3Success)
  274.         return true;
  275.     else
  276.         return false;
  277. }
  278.